package top.kaoshanji.p2ch19a.support;

import java.util.concurrent.CountDownLatch;

/**
 * 工作线程
 * author kaoshanji
 * ^_^
 * create time 2025/2/27 19:39
 */
public class Worker extends Thread {

    private CountDownLatch latch;

    public Worker(CountDownLatch latch) {
        this.latch = latch;
    }

    @Override
    public void run() {
        try {
            // simulate working on task
            Thread.sleep((int) (Math.random() * 1000));

            // simulate exception
            if (Math.random() < 0.02) {
                throw new RuntimeException("bad luck");
            }

        } catch (InterruptedException e) {
        } finally {
            this.latch.countDown();
        }
    }
}
